package edu.ynu.dao;
// default package

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import edu.ynu.po.Order;

/**
 	* A data access object (DAO) providing persistence and search support for Order entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Order
  * @author MyEclipse Persistence Tools 
 */

public class OrderDAO extends BaseHibernateDAO  {
		 private static final Log log = LogFactory.getLog(OrderDAO.class);
		//property constants
	public static final String CUSTOMER_ID = "customerId";
	public static final String DATE = "date";
	public static final String ADRESS = "adress";
	public static final String STATUS = "status";



    
    public void save(Order transientInstance) {
        log.debug("saving Order instance");
        try {
        	Session session = getSession();
        	Transaction trans = session.beginTransaction();
            session.save(transientInstance);
            trans.commit();
            session.clear();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Order persistentInstance) {
        log.debug("deleting Order instance");
        try {
        	Session session = getSession();
        	Transaction trans = session.beginTransaction();
            session.delete(persistentInstance);
            trans.commit();
            session.clear();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	public void update(Order persistentInstance) {
        log.debug("updating Order instance");
        try {
        	Session session = getSession();
        	Transaction trans = session.beginTransaction();
            session.update(persistentInstance);
            trans.commit();
            session.clear();
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }
    
    public Order findById( java.lang.String id) {
        log.debug("getting Order instance with id: " + id);
        try {
            Order instance = (Order) getSession()
                    .get("edu.ynu.po.Order", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Order instance) {
        log.debug("finding Order instance by example");
        try {
            List results = getSession()
                    .createCriteria("Order")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Order instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Order as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByCustomerId(Object customerId
	) {
		return findByProperty(CUSTOMER_ID, customerId
		);
	}
	
	public List findByDate(Object date
	) {
		return findByProperty(DATE, date
		);
	}
	
	public List findByAdress(Object adress
	) {
		return findByProperty(ADRESS, adress
		);
	}
	
	public List findByStatus(Object status
	) {
		return findByProperty(STATUS, status
		);
	}
	

	public List findAll(Map paramMap) {
		log.debug("finding all Order instances");
		try {
			String orderId = (String)paramMap.get("orderId");
			String customerId = (String)paramMap.get("customerId");
			String magazineId = (String)paramMap.get("magazineId");
			Criteria c = getSession().createCriteria(Order.class);
			if(StringUtils.isNotEmpty(orderId)){
				c.add(Expression.like("orderId", "%"+orderId+"%"));
			}
			if(StringUtils.isNotEmpty(customerId)){
				c.add(Expression.like("customerId", "%"+customerId+"%"));
			}
			if(StringUtils.isNotEmpty(magazineId)){
				c.add(Expression.like("date", "%"+magazineId+"%"));
			}
			return c.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Order merge(Order detachedInstance) {
        log.debug("merging Order instance");
        try {
            Order result = (Order) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Order instance) {
        log.debug("attaching dirty Order instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Order instance) {
        log.debug("attaching clean Order instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}